<template>
  <div class="app-container">
    <el-card class="box-card search-div" v-show="showSearch">
      <el-form :inline="true" :model="listQuery" ref="queryForm">
        <el-form-item label="模块">
          <el-input placeholder="请输入查询的模块" v-model="listQuery.roleName" clearable/>
        </el-form-item>
        <el-button type="primary" @click="handleSearch" icon="el-icon-search">搜索</el-button>
        <el-button @click="handleResetQuery" icon="el-icon-refresh">重置</el-button>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" v-hasPermi="['sys_role_add']" class="el-icon-plus" @click="add">新增</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </div>
        <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row class="table-list">
          <el-table-column label="序号" type="index" min-width="5%"/>
          <el-table-column  align="center" width="200" label="角色名称" prop="roleName" >
          </el-table-column>
          <el-table-column  align="center" width="100" label="角色编码" prop="roleCode" >
          </el-table-column>
          <el-table-column align="center" label="角色描述" prop="remark">
          </el-table-column>
          <el-table-column align="center" label="更新时间"  width="160" prop="updateTime">
          </el-table-column>
          <el-table-column align="center" label="操作" width="240">
            <template slot-scope="scope">
              <el-button plain v-hasPermi="['sys_role_edit']" type="violet" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button plain v-hasPermi="['permission_menu']" type="success" @click="openMenuPermissions(scope.row)">功能授权</el-button>
              <el-button plain v-hasPermi="['sys_role_remove']" type="danger" @click="removeById(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </el-card>
    <!--弹出框-->
    <el-dialog :title="dialogTitle" :width="'600px'" :visible.sync="showDialog" :close-on-click-modal="false">
      <el-form :rules="rules" ref="saveSysRoleData" :model="saveSysRoleData" label-width="100px">
        <el-form-item label="角色名称:" prop="roleName">
          <el-input v-model="saveSysRoleData.roleName" :maxlength="40"  placeholder="最多输入40个字符" clearable show-word-limit/>
        </el-form-item>
        <el-form-item label="角色编码:" prop="roleCode">
          <el-input v-model="saveSysRoleData.roleCode" :maxlength="40"  placeholder="最多输入40个字符" clearable show-word-limit/>
        </el-form-item>
        <el-form-item label="角色描述:" prop="remark">
          <el-input type="textarea" rows="3" v-model="saveSysRoleData.remark" :maxlength="200"  placeholder="最多输入200个字符" clearable show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button type="primary" @click="onSave('saveSysRoleData')">确 定</el-button>
      </div>
    </el-dialog>
    <!--功能授权-->
    <el-dialog title="功能授权" width="900px" :visible.sync="showMenuPermissionsDialog" :close-on-click-modal="false">
      <el-table :data="menuDatas" row-key="id" border default-expand-all style="width: 100%" height="400">
        <el-table-column align="left" property="menuName" label="名称" width="180"></el-table-column>
        <el-table-column align="left" property="funs" label="功能">
          <template slot-scope="scope">
            <el-checkbox-group v-model="permissionsIds">
              <el-checkbox :label="scope.row.id" :key="scope.row.id">查看</el-checkbox>
              <el-checkbox v-for="city in scope.row.funs" :label="city.id" :key="city.id">{{city.menuName}}</el-checkbox>
            </el-checkbox-group>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showMenuPermissionsDialog = false">取 消</el-button>
        <el-button type="primary" @click="onMenuPermissionsSave()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as sysRole from './js/role'
import { mapGetters } from 'vuex'

export default {
  name: 'sysRoleList',
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      // 显示搜索条件
      showSearch: true,
      listQuery: {
        page: 1, limit: 10, roleName: ''
      },
      showDialog: false,
      saveSysRoleData: {
        id: '',roleName: '',roleCode:'',remark: ''
      },
      rules: {
        roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      },
      dialogTitle: '添加角色',
      curRow:{},
      showMenuPermissionsDialog:false,
      menuDatas: [],
      permissionsIds: []
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      sysRole.fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleResetQuery(){
      this.resetForm("queryForm")
      this.handleSearch()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    //删除
    removeById(id) {
      this.$confirm('请确定是否删除该角色', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        sysRole.removeById(id).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getList()
        })
      }).catch(function(error) { console.log(error) })
    },
    // 新增
    add(pit, index) {
      this.showDialog = true
      this.dialogTitle = '添加角色'
      // 清空表单数据
      this.$nextTick(() => {
        this.resetForm('saveSysRoleData')
      })
    },
    // 编辑
    handleEdit(index, row) {
      this.showDialog = true
      this.dialogTitle = '编辑角色'
      this.$nextTick(() => {
        this.resetForm('saveSysRoleData')
        this.saveSysRoleData.id = row.id
        this.saveSysRoleData.roleName = row.roleName
        this.saveSysRoleData.roleCode = row.roleCode
        this.saveSysRoleData.remark = row.remark
      })
    },
    // 重置表单 解决验证提示的问题
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 保存
    onSave(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.showDialog = false
          sysRole.save(this.saveSysRoleData).then((response) => {
            this.$message({ type: 'success', message: '操作成功!' })
            this.getList()
          }).catch(function(error) {
            console.log(error)
          })
        }
      })
    },
    openMenuPermissions(row) {
      this.curRow = row
      this.showMenuPermissionsDialog = true
      sysRole.getMenuPermissionsById(row.id).then(response => {
        this.menuDatas = response.data.menuDatas
        this.permissionsIds = response.data.permissionsIds
      })
    },
    onMenuPermissionsSave() {
      sysRole.saveMenuPermission(this.curRow.id, this.permissionsIds.join(',')).then(response => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.showMenuPermissionsDialog = false
          this.getList()
      })
    }
  }
}

</script>
